package com.free.dao;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import com.free.utils.RRException;

import java.util.Set;

/**
 *  参考文档：http://docs.jboss.org/hibernate/validator/5.4/reference/en-US/html_single/
* @ClassName: ValidatorUtils 
* @Description: TODO(hibernate-validator校验工具类) 
* @author wujie
* @email 602991693@qq.com 
* @date 2017年8月31日 上午10:30:26 
*
 */
public class ValidatorUtils {
	private static Validator validator;
	
	static{
		validator = Validation.buildDefaultValidatorFactory().getValidator();
	}
	/**
	 * 
	* @Title: validateEntity 
	* @author: wujie
	* @email: 602991693@qq.com
	* @Description: TODO(校验对象) 
	* @param @param object 待校验对象
	* @param @param groups 待校验的组
	* @param @throws RRException   校验不通过则报RRException异常 
	* @return void    返回类型 
	* @throws
	 */
	public static void validateEntity(Object object,Class<?>... groups) throws RRException{
		Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
		if(!constraintViolations.isEmpty()){
			ConstraintViolation<Object> constraint = (ConstraintViolation<Object>)constraintViolations.iterator().next();
			throw new RRException(constraint.getMessage());
		}
	}
}
